home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / nn.zip / PACK_DAT.C < prev    next >
C/C++ Source or Header  |  1989-12-29  |  2KB  |  109 lines

  1. #include "config.h"
  2.  
  3. /* #define DATE_TEST /* never define this !! */
  4.  
  5. /*
  6.  *    Calculate an approximate "time_stamp" value for a date
  7.  *    string.  The actual value is not at all critical, 
  8.  *    as long as the "ordering" is ok.
  9.  *
  10.  *    The result is NOT a time_t value, i.e. ctime() will
  11.  *    not produce the original Date string.
  12.  *
  13.  *    The date must have format:  [...,] [D]D Mmm YY hh:mm:ss GMT
  14.  */
  15.  
  16. pack_date(destp, date)
  17. time_stamp *destp;
  18. char *date;
  19. {
  20.     register time_stamp res;
  21.     register int min, hour, day, mon, year;
  22.     
  23.     *destp = 0;
  24.     if (date == NULL) return;
  25.     
  26.     if ((day = next_int(&date)) == 0) return;
  27.  
  28.     while (*date && isspace(*date)) date++;
  29.     
  30.     switch (*date++) {
  31.      case 'J':
  32.     if (*date++ == 'a') { mon = 0; break; }
  33.     if (*date++ == 'n') { mon = 5; break; }
  34.     mon = 6; break;
  35.      case 'F':
  36.     mon = 1; break;
  37.      case 'M':
  38.     if (*++date == 'r') { mon = 2; break; }
  39.     mon = 4; break;
  40.      case 'A':
  41.     if (*date++ == 'p') { mon = 3; break; }
  42.     mon = 7; break;
  43.      case 'S':
  44.     mon = 8; break;
  45.      case 'O':
  46.     mon = 9; break;
  47.      case 'N':
  48.     mon = 10; break;
  49.      case 'D':
  50.     mon = 11; break;
  51.      default:
  52.     return;
  53.     }
  54.     
  55.     year = next_int(&date);
  56.     hour = next_int(&date);
  57.     min = next_int(&date);
  58.     
  59.     year -= 87;    /* base is 1987 */
  60.     if (year < 0) year += 100;
  61.     
  62.     res = (year * 12 + mon) * 31 + day - 1;
  63.     res *= 24 * 60;
  64.     res += (hour * 60) + min;
  65.  
  66.     *destp = res;
  67. }
  68.  
  69.  
  70. static next_int(dp)
  71. char **dp;
  72. {
  73.     register char *str = *dp;
  74.     register i;
  75.  
  76.     while (*str && !isdigit(*str)) str++;
  77.     
  78.     i = 0;
  79.     while (*str && isdigit(*str))
  80.     i = (i * 10) + *str++ - '0';
  81.     
  82.     *dp = str;
  83.     return i;
  84. }
  85.  
  86.  
  87. #ifdef DATE_TEST
  88.  
  89.  
  90. main()
  91. {
  92.     char buffer[128];
  93.     char *dp;
  94.     unsigned long t;
  95.     
  96.     while (fgets(buffer, 128, stdin)) {
  97.     dp = strchr(buffer, ':');
  98.     if (dp == NULL) continue;
  99.     dp++;
  100.     while (isspace(*dp)) dp++;
  101.     pack_date(&t, dp);
  102.     printf("%lu\t%s\n", t, dp);
  103.     }
  104.     
  105.     nn_exit(0);
  106. }
  107.  
  108. #endif
  109.